package app.qrcode.browser.todoapp.fragments.update

import android.app.DatePickerDialog
import android.app.Dialog
import android.os.Bundle
import android.util.Log
import android.widget.DatePicker
import androidx.fragment.app.DialogFragment
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModelProvider
import app.qrcode.browser.todoapp.data.viewmodel.ToDoViewModel
import app.qrcode.browser.todoapp.utils.dateFormater
import java.util.*

/**
 * Created by YiGo on 2022/9/19.
 */
class DatePickerFragment : DialogFragment(), DatePickerDialog.OnDateSetListener {
    lateinit var dateChosen: DateChosen

    @JvmName("setDateChosen1")
    fun setDateChosen(callback: DateChosen) {
        this.dateChosen = callback
    }


    interface DateChosen {

        fun setDate(str: String)

    }


    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        // Use the current date as the default date in the picker
        val c = Calendar.getInstance()
        val year = c.get(Calendar.YEAR)
        val month = c.get(Calendar.MONTH)
        val day = c.get(Calendar.DAY_OF_MONTH)

        // Create a new instance of DatePickerDialog and return it
        return DatePickerDialog(this.requireActivity(), this, year, month, day)
    }

    override fun onDateSet(view: DatePicker, year: Int, month: Int, day: Int) {
//        val dateStr: String = dateFormater.format(System.currentTimeMillis())
        var currentMonth = month+1
        var yearstr = year.toString()
        var monthstr = if (currentMonth.toString().length < 2) "0$currentMonth" else currentMonth.toString()
        var dayStr = if (day.toString().length < 2) "0$day" else day.toString()
        dateChosen.setDate("$yearstr-$monthstr-$dayStr")
    }
}